package com.guhappy.action.stock;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.guhappy.model.user.ContestStockRecord;
import com.guhappy.model.user.GlobalStock;
import com.guhappy.model.user.GlobalStockHome;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("default")
@Action(value="stock",results={@Result(name="success", location="/pages/stockView.jsp"),@Result(name="error", location="/pages/homePage.jsp")})
public class Stock extends ActionSupport implements ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String area;
	private String stock;
	private ContestStockRecord stockrecord = new ContestStockRecord();
	private GlobalStock stockbean = new GlobalStock();
	
	HttpServletRequest request;
	
	public ContestStockRecord getStockrecord() {
		return stockrecord;
	}

	public void setStockrecord(ContestStockRecord stockrecord) {
		this.stockrecord = stockrecord;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
	
	public HttpServletRequest getServletRequest() {
		return this.request;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getStock() {
		return stock;
	}

	public void setStock(String stock) {
		this.stock = stock;
	}

	public GlobalStock getStockbean() {
		return stockbean;
	}

	public void setStockbean(GlobalStock stockbean) {
		this.stockbean = stockbean;
	}

	@Override
	public String execute() throws Exception {
		String sticker = stock.substring(0,stock.indexOf("\t")).trim();
		stockbean = GlobalStockHome.findByTicker(sticker);
		if (stockbean ==null){
			return ERROR;
		}
		stockbean.setChartUrlSmall("http://ichart.finance.yahoo.com/t?s=" + stockbean.getSticker());
		stockbean.setChartUrlLarge("http://chart.finance.yahoo.com/w?s=" + stockbean.getSticker());     
		stockrecord = GlobalStockHome.getStockRecord(stockbean.getId());
		request.setAttribute("stockbean", stockbean);
		
		return SUCCESS;
	}

}
